<?php

class Auto_Controller_Simple extends Zend_Controller_Action
{
    protected $_model;
    protected $_page;
    protected $_title;

    protected function _noLayout()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
    }

    public function indexAction()
    {
        $this->view->items = $this->_model->getList();
        $this->view->page = $this->_page;
        $this->view->title = $this->_title;

        $this->view->error = $this->_getParam('error');

        $this->_helper->viewRenderer->setNoRender();
        echo $this->view->render('simple/simple.phtml');
    }

    public function addAction()
    {
        $this->_noLayout();

        try {
            $this->_model->create(trim($this->getRequest()->getParam('name')));
        } catch (Zend_Db_Exception $e) {}

        $this->_helper->redirector('index', $this->_page);
    }

    public function editAction()
    {
        $this->_noLayout();

        try {
            $this->_model->edit(
                $this->getRequest()->getParam('id'),
                trim($this->getRequest()->getParam('name'))
            );
        } catch (Zend_Db_Exception $e) {
            echo 'error';
        }
    }

    public function removeAction()
    {
        $this->_noLayout();

        $params = array();
        try {
            $this->_model->remove($this->getRequest()->getParam('id'));
        } catch (Zend_Db_Exception $e) {
            $params['error'] = 'За да изтриете записа, трябва да изтриете всички зависими записи на автомобили';
        }

        $this->_helper->redirector('index', $this->_page, null, $params);
    }
}
